$(document).ready(
				function() {
//					var ue=UE.getEditor('ueditorDetails');
	
					//获取项目名
					var contextPath = document.location.pathname; 
					var index =contextPath.substr(1).indexOf("/"); 
					contextPath = contextPath.substr(0,index+1); 
					
					var totalCount = $("#totalCount").val();
					var pSize = $("#pageSize").val();;
					var pageIndex = $("#pageIndex").val();
					var pageCount = $("#pageCount").val();
					

					$("#giDetails").on('hidden.bs.modal', function() {
						$(this).removeData("bs.modal");
					});

					$("#optBtn")
							.delegate(
									'#saveBtn','click',
									function() {
										
										$("#giftInfo").attr("method","post");
										
										var giId = $("#giId").val();
									
											if (giId =="") {
												$("#giftInfo").attr("action","save.do");
											} else {
												$("#giftInfo").attr("action","update.do");
											}
											$("#giftInfo").submit();
										
									});

					$('#pagination2').jqPaginator(
									{
										 totalPages: pageCount,
										    visiblePages: 10,
										    currentPage: 1,
										
										    prev: '<li class="prev"><a href="javascript:void(0);">上一页</a></li>',
										    page: '<li class="page"><a href="javascript:void(0);">{{page}}</a></li>',
										    next: '<li class="next"><a href="javascript:void(0);">下一页</a></li>',
										onPageChange : function(num, type) {
											$("#pageIndex").val(num);
											var queryUrl = "queryByPage.do";
											$.ajax({
														url : queryUrl,
														dataType : 'json',
														data : {
															pageIndex :num,
															pageSize : pSize
														},
														cache : false,
														success : function(data) {
															
															$("#giList tr:not(:first)").remove();
															var results = data.pageResult.resultList;
															for (i = 0; i < results.length; i++) {
																var lg = results[i];
																var rowContent = "<tr>"
																	+ "<td><input name='giId' id='giId' class='checkbox' type='checkbox' value='"
																	+ lg.id
																	+ "'/></td>"
																	+ "<td>"
																	+ lg.giftCode
																	+ "</td>"
																	+ "<td>"
																	+ lg.giftName
																	+ "</td>"
																	
																	/*+ "<td><div style=' width:100px; height:30px;overflow:hidden;'><img src='"+contextPath+"/HRTWEB/images/23/" 
																	+lg.pic 
																	+ "'border='0' width='100px' />"
																	+ "</div></td>"
																	*/
																	+ "<td><div style=' width:100px; height:30px;overflow:hidden;'>" 
																	+ "<a href = 'javascript:void(0)' onclick = 'aa();'>" 
																	+ "<img src='"+contextPath+"/HRTWEB/images/23/" 
																	+lg.pic 
																	+ "' border='0' width='100px' />"
																	+ "</a></div>" 
																	+ "<div id='light' class='white_content'>  " 
																	+ "<a href = 'javascript:void(0)' onclick = 'bb();'> " 
																	+ "<img src='"+contextPath+"/HRTWEB/images/23/" 
																	+ lg.pic 
																	+ "' /> </a> </div>  " 
																	+ "<div id='fade' class='black_overlay'></div>" 
																	+ "</td>"
																	
																	+ "<td>"
																	+ lg.promotion
																	+ "</td>"
																	+ "<td>"
																	+ lg.discount
																	+ "</td>"
																	+ "<td>"
																	if (lg.ispush == 0) {
																		rowContent = rowContent
																				+ "下架";
																	} else {
																		rowContent = rowContent
																		+ "<font color='red'>上架</font>";
																	}
																	+ "</td>";
																	
																	rowContent = rowContent
																	+ "<td>" 
																	+ "<button name='viewRule' objId='"
																	+ lg.id 
																	+ "' class='btn btn-sm-block'>查看规格库</button>" 
																	+ "</td>"
																	
																	+ "<td>" 
																	+ "<button name='editBtn' id='giBtn' objId='"
																	+ lg.id
																	+ "' class='btn btn-sm-block'>修改</button>" 
																	+ "</td>" 
																	+ "<td>"
																	+ "<button id='giBtn' name='checkBtn' objId='"
																	+ lg.id
																	+ "' class='btn btn-sm-block checkBtn'>查看</button></td>"
																	+ "</tr>";
																$("#giList")
																		.append(
																				rowContent);
															}

														},
														error : function(html) {
															return;
														}
													});
										}
									});

					/**
					 * 全选当前页记录
					 */
					$("#giList").delegate('#selectAll', 'click', function() {
						var isSelectAll = $(this).prop('checked');
						$("input[name='giId']").each(function() {
							$(this).prop("checked", isSelectAll);
						});
					});
					
					/**
					 * 查看规格库按钮事件
					 */

					$("#giList").delegate(
							'[name="viewRule"]',
							'click',
							function() {
								var giId = $(this).attr("objId");

								var editUrl = contextPath+"/manage/giftDetail/list.do";
								document.location.href = editUrl + "?id=" + giId;
							});
					
					/**
					 * 修改按钮事件
					 */
					
					$("#giList").delegate('#giBtn', 'click', function() {
						$("#checks input").removeAttr("readonly");
						$("#checks textarea").removeAttr("readonly");
						$("#checks select").removeAttr("disabled");
						$("#ueditorDetails").removeAttr("readonly");
						
						var giId = $(this).attr("objId");
						
						var editUrl = "show.do?giId=" + giId;
						$.ajax({
							
							url : editUrl,
							dataType : 'json',
							data : {
								id : giId
							},
							cache : false,
							
							success : function(data) {
								
								loadGI(data);
							},
							error : function(html) {
								alert("Load Data failly");
								refresh();
							}
						});
						
					});
					
					
					
					/**
					 * 查看详情
					 */
					$("#giList").delegate('.checkBtn', 'click', function() {
						
						$("#checks input").attr({ readonly: 'true' });
						$("#checks textarea").attr({ readonly: 'true' });
						$("#checks select").attr("disabled","disabled");
//						$("#ueditorDetails").attr({ readonly: 'true' });
//						var uue = new UE.ui.Editor({readonly:'true'});
//						uue.render('ueditorDetails');
//						UE.ui.Editor({readonly:'true'}).render('ueditorDetails');
						
						
					});

					
					/**
					  删除按钮事件
					 */
					$("#tools").delegate(
						'#deleteBtn',
						'click',
						function() {
							delChk = $('input[name="giId"]:checked');

							if (delChk.length > 0) {
								if (confirm("Are you sure delete the record?")) {

									var ids = "";
									var delUrl = "delete.do";
                                    var idArr=new Array();
                                    
                                    for (var i = 0; i < delChk.length; i++) {
                                    	idArr.push(delChk.get(i).value);
                               
								}
									
									$.ajax({
												url : delUrl,
												type: "POST",
												data : {
													ids : idArr.toString()
												},
												cache : false,

												success : function(
														html) {
													alert("Delete successfully");
													refresh();
												},
												error : function(
														html) {
													alert("Delete failly");
													refresh();
												}
											});
								} else {
									return;
								}
							} else {
								alert("Please select the record to delete");
								return;
							}
						});
					
					/**
					  上架按钮事件
					 */
					$("#tools") .delegate(
						'#shelveBtn',
						'click',
						function() {
							sheChk = $('input[name="giId"]:checked');

							if (sheChk.length > 0) {
								if (confirm("你确定要将礼品上架?")) {

									var ids = "";
									var sheUrl = "shelve.do";
                                  var idArr=new Array();
                                 
                                  
                                  for (var i = 0; i < sheChk.length; i++) {
                                  	idArr.push(sheChk.get(i).value);
                             
								}
									
									$.ajax({
												url : sheUrl,
												type: "POST",
												data : {
													ids : idArr.toString()
												},
												cache : false,

												success : function(
														html) {
													alert("上架成功");
													refresh();
												},
												error : function(
														html) {
													alert("上架失败");
													refresh();
												}
											});
								} else {
									return;
								}
							} else {
								alert("请选择要上架的礼品");
								return;
							}
						});
					
					/**
					  下架按钮事件
					 */
					$("#tools")
					.delegate(
							'#unShelveBtn',
							'click',
							function() {
								unSheChk = $('input[name="giId"]:checked');
								
								if (unSheChk.length > 0) {
									if (confirm("你确定要将礼品下架?")) {
										
										var ids = "";
										var unSheUrl = "unShelve.do";
										var idArr=new Array();
										
										
										for (var i = 0; i < unSheChk.length; i++) {
											idArr.push(unSheChk.get(i).value);
											
										}
										
										$.ajax({
											url : unSheUrl,
											type: "POST",
											data : {
												ids : idArr.toString()
											},
											cache : false,
											
											success : function(
													html) {
												alert("下架成功");
												refresh();
											},
											error : function(
													html) {
												alert("下架失败");
												refresh();
											}
										});
									} else {
										return;
									}
								} else {
									alert("请选择要下架的礼品");
									return;
								}
							});
					
					
					/**
					 * 添加
					 */
					$("#tools").delegate('#addGiftInfo', 'click', function() {
						$("#checks textarea").removeAttr("readonly");
						$("#giDetails input").each(function() {
							$(this).prop("value", null);
							$(this).prop("readonly", false);

						});

						$("#giDetails textarea").each(function() {
							$(this).prop("value", null);

						});
						$("#ueditorDetails").each(function() {
							UE.getEditor('ueditorDetails').setContent("");
							
						});
						
						$("#giDetails select").each(function() {
							$(this).prop("value", 0);
							
						});
						$("#giDetails").modal('show');
						$("#ueditorDetails").modal('show');

					});
					
					/*ue.ready(function() {
						//设置编辑器的内容
						//获取html内容，返回<p>....</p>
					
						
					});*/
					/**
					 * 修改页面上的默认值
					 */
					function loadGI(giftInfo) {
						$("#giId").val(giftInfo.id);
						$("#giftCode").val(giftInfo.giftCode);
						$("#giftName").val(giftInfo.giftName);
						$("#promotion").val(giftInfo.promotion);
						$("#discount").val(giftInfo.discount);
						$("#pic").val(giftInfo.pic);
						$("#unit").val(giftInfo.unit);
						$("#enterPrice").val(giftInfo.enterPrice);
						$("#salePrice").val(giftInfo.salePrice);
						$("#groupPrice").val(giftInfo.groupPrice);
						$("#mailPrice").val(giftInfo.mailPrice);
						$("#totals").val(giftInfo.totals);
						$("#clickCount").val(giftInfo.clickCount);
						$("#collection").val(giftInfo.collection);
						$("#detail").val(giftInfo.detail);
						$("#description").val(giftInfo.description);
						$("#isGroup").val(giftInfo.isGroup);
						$("#isPackage").val(giftInfo.isPackage);
						$("#isPromote").val(giftInfo.isPromote);
						$("#isMail").val(giftInfo.isMail);
						$("#ispush").val(giftInfo.ispush);

						//富文本编辑器
						UE.getEditor('ueditorDetails').setContent(giftInfo.details_Str);
						
						$("#giDetails").modal('show');
					}

					function vaildate(form) {
						return true;
					}
					/**
					 * 刷新页面
					 * */
					function refresh() {
						window.location.reload();
					}
				});





